var outerLayout, innerLayout;
var dataTableObj =  new dataTableObj();
$(document).ready(function (){
    
    try {
        $("body select").msDropDown();
    } catch(e) {
        alert(e.message);
    }
    
    $('.clear').live('click',function(){
    var parent = $(this).parents('table:eq(0)');
    $(parent).find(":input:not(:radio)").val('');
    $(parent).find(":input:not(:radio):not(:button)").css('background-color','transparent');
    $(parent).find(".save").html(NAME_INSERT);
    $(parent).find(":checkbox").attr('checked',false); 
   });
    
    //Start khi click vào nút save dùng để thêm hoặc cập nhật    
    $('.save').live('click',function(){
        
        var parent = $(this).parents('table:eq(0)');
        $(parent).find(":input:not(:radio):not(:button)").css('background-color','transparent');  
        var arr = new Array();
        arr = $(this).attr('id').split('_');         
        var funcname = 'xajax_'+arr[0]+'_save';
        if(checkValidate1(this)){
            var params = new Array();
            var arguments = getParams(this);
            params[0]=arguments;
            changeWaiting(0); 
            call_user_func_array(funcname,params);
        }        
    });
    
    $('.cancelDialog').live('click',function(){
       // alert($(this).attr('id'));
       $(this).parents('.ui-dialog-content:eq(0)').dialog('close');
    });
    //End khi click vào nút save dùng để thêm hoặc cập nhật    

    //Start khi click vào biểu tượng edit  
    $('.edit').live('click',function(){
        // if($(this).hasClass('custom')){
        
            var tbl = $(this).parents('table:eq(1)');
            var arr = new Array();
            arr = $(tbl).attr('id').split('_'); 
            var tr = $(this).parents('tr:eq(0)');
            var val = $(tr).find('input:eq(0)').val();        
/*            var funcname = 'xajax_'+arr[0]+'_edit';
            $('#'+arr[0]+'_hdEdit').val(1);
            $('#'+arr[0]+'_hdId').val(val);*/
            nameId = arr[0];
            $('#'+nameId+'_info').find(":input:not(:radio):not(:button)").css('background-color','transparent');                      
            var funcname = 'xajax_'+nameId+'_edit';
            $('#'+nameId+'_hdEdit').val(1);
            $('#'+nameId+'_hdId').val(val);
            $('#'+nameId+'_submit').html(NAME_UPDATE);
            var params = new Array();
            params[0] = val;
            changeWaiting(0);
            call_user_func_array(funcname,params);
       
    });
    
    $('.export_excel').live('click',function(){
       var formid =  $(this).parents('form:eq(0)').attr('id');
       export_excel(formid);
     
    });
    
    $('.export_pdf').live('click',function(){
       var formid =  $(this).parents('form:eq(0)').attr('id');
       export_pdf(formid);
     
    });
    
    
    
    //End khi click vào biểu tượng edit

    //Start khi click vao tạo report
    $('.search').live('click',function(){
        var arr = new Array();
        arr = $(this).attr('id').split('_');         
        var funcname = 'xajax_'+arr[0]+'_search';
        if(checkValidate1(this)){
            var params = new Array();
            var arguments = getParams(this);
            params[0]=arguments;
            changeWaiting(0); 
            call_user_func_array(funcname,params);
        }        
    });
    
    $('.viewchart').live('click',function(){
        var arr = new Array();
        arr = $(this).attr('id').split('_');         
        var funcname = 'xajax_'+arr[0]+'_viewchart';
        if(checkValidateReport(this)){
            var params = new Array();
            var arguments = getParams(this);
            params[0]=arguments;
            changeWaiting(0); 
            call_user_func_array(funcname,params);
        }        
    });
    
    //End khi click vao tạo report
    
    // Start khi click vao cac 
    $('.active').live('click',function(){
        var img = $(this).attr('src');
        var lastimg = 'adagps.com/images/icons/active1.gif';
        if(img=='adagps.com/images/icons/active1.gif'){
            lastimg = 'adagps.com/images/icons/active0.gif';
        }
        $(this).attr('src',lastimg);
        var trparent = $(this).parents('tr:eq(0)');
        id = $(trparent).find('input:first').val();
        //alert(id);
        var tblparent = $(this).parents('table:eq(1)');
        var arr = new Array();
        var name =$(tblparent).attr('id');
        arr = name.split('_');
        frm = arr[0];
        changeWaiting(0);
        hasChange = true;
        if($(this).hasClass('custom')){
           var funcname = 'xajax_'+frm+'_active';
           
            var params = new Array();
            params[0] = id;
            //alert(funcname);
            call_user_func_array(funcname,params); 
        }else{
            //alert(frm);
            if($(this).hasClass('protected')){
              xajax_ajxActive(id,frm,1);  
            }else{
              xajax_ajxActive(id,frm);    
            }
            
        }   
    });

    $('.dataTables_length').find('select').live('change',function(){
        checkOne(); 
    });

    $('.deleteAll').live('click', function() {
        var parent =  $(this).parents('div.divGrid');
        var id =  $(parent).attr('id');
        var arr = new Array();
        arr = id.split('_');
        var prefix = arr[0];
        strid ='';
        $('#' + id + ' div.dataTables_scrollBody tr:not([th])').find(':input[type=checkbox]').each(function(){
            if($(this).attr('checked')){
                strid += ','+$(this).val();
            }
        });
        if(strid.length==0){
            alert(PLEASE_CHOOSE_ITEMS_TO_DELETE);
            return false;
        }else{
            if(!confirm(DELETE_MANY_CONFIRM)){
            return false;
            }
        }
        changeWaiting(0);        
        strid= strid.substr(1);
        hasChange = true;
        if($(this).hasClass('custom')){
           var funcname = 'xajax_'+arr[0]+'_delete';
            var params = new Array();
            params[0] = strid;
            call_user_func_array(funcname,params); 
        }else if($(this).hasClass('clscommon')){
            xajax_ajxdeleteCommonData(strid,prefix);
        }else{
           xajax_ajxdeleteData(strid,prefix); 
        }
        
    } );

    $('.checkAll').live('click',function(){
        var tblparent = $(this).parents('div.divGrid');
        var test = ($(this).attr('checked')=='checked'||$(this).attr('checked')==true)?true:false;  
        $(tblparent).find('.checkOne').each(function(){
         $(this).attr('checked',test); 
        }) ;     
        /*if(test){
        $(tblparent).find('.deleteAll').css('display','');  
        } else{
        $(tblparent).find('.deleteAll').css('display','none'); 
        }  */
        filterChange();   
    });

    $('.checkOne').live('change',function(){
        var tblparent = $(this).parents('table:eq(1)');
        var test = $(this).attr('checked');
        var display = 'none';  
        $(tblparent).find('.checkOne').each(function(){
        if(!$(this).attr('checked')){
            test=false; 
        }else{
            display = '';
        }
        });
        $(tblparent).find('.deleteAll').css('display',display);
        $(tblparent).find('.checkAll').attr('checked',test);
        var tr = $(this).parent().parent();
        if ( $(this).attr("checked") )
        {
            $(tr).addClass("row_selected");
        }
        else
        {  
            $(tr).removeClass("row_selected");
        }                                           
    });

    $('.fg-button').live('click',function(){
        
        $('div.divGrid').each(function(){
        var display = 'none';
        var chkall = true;
        
        $(this).find('.checkOne').each(function(){
            if($(this).attr('checked')){
                display = '';  
            }else{
               chkall= false; 
            }           
        });
        //$('.deleteAll').css('display',display);
        $(this).find('.checkAll').attr('checked',chkall);
        });     
    });

//    outerLayout = $('body').layout({ 
//        center__paneSelector:      ".ui-outer-center" 
//        ,  west__paneSelector:        ".ui-outer-west"  
//        ,  west__size:                250 
//        ,  spacing_open:              0 // ALL panes
//        ,  spacing_closed:            0 // ALL panes
//        ,  west__maxSize:             400
//        ,  center__onresize:   "innerLayout.resizeAll"
//        ,  applyDefaultStyles:        true
//    }); 

    $("ul.menu_body li:even").addClass("alt");
    $('div.menu_head').click(function () {
        if($('#hdmenu').val()!='0'){
            $($('#hdmenu').val()).slideToggle('high');  
        }
        var menushow =  '#menu_body'+$(this).attr('id');
        if($('#hdmenu').val()!=menushow){
            $(menushow).slideToggle('high');
            $('#hdmenu').val(menushow); 
        }else{
            $('#hdmenu').val(0); 
        }
    });

    $('div.menu_head_select').click(function () {
        if($('#hdmenu').val()!='0'){
            $($('#hdmenu').val()).slideToggle('high');  
        }
        var menushow =  '#menu_body'+$(this).attr('id');
        $(menushow).removeClass('disblock');
        if($('#hdmenu').val()!=menushow){
            $(menushow).slideToggle('high');
            $('#hdmenu').val(menushow); 
        }else{
            $('#hdmenu').val(0); 
        }
    });
//
    $('ul.menu_body li a').mouseover(function () {
        $(this).animate({ fontSize: "12px", paddingLeft: "10px" }, 10 );
    });
    $('ul.menu_body li a').mouseout(function () {
        $(this).animate({ fontSize: "12px", paddingLeft: "10px" }, 10 );
    });

    

});



$(function() {
            $( "#divTab1" ).tabs({
                cache : false,
                ajaxOptions: {
                    error: function( xhr, status, index, anchor ) {
                    $( anchor.hash ).html(
                    "Couldn't load this tab. We'll try to fix this as soon as possible. " +
                    "If this wouldn't be a demo." );
                    }
                }
            });
            
            $( "#divTab2" ).tabs({
                cache : false,
                ajaxOptions: {
                    error: function( xhr, status, index, anchor ) {
                    $( anchor.hash ).html(
                    "Couldn't load this tab. We'll try to fix this as soon as possible. " +
                    "If this wouldn't be a demo." );
                    }
                }
            });
            $( "#divTab3" ).tabs({
                cache : false,
                ajaxOptions: {
                    error: function( xhr, status, index, anchor ) {
                    $( anchor.hash ).html(
                    "Couldn't load this tab. We'll try to fix this as soon as possible. " +
                    "If this wouldn't be a demo." );
                    }
                }
            });
            
            $( "#divTab4" ).tabs({
                cache : false,
                ajaxOptions: {
                    error: function( xhr, status, index, anchor ) {
                    $( anchor.hash ).html(
                    "Couldn't load this tab. We'll try to fix this as soon as possible. " +
                    "If this wouldn't be a demo." );
                    }
                }
            });
});

function initDivleft(){
    height = $(window).height()-75;
    height =  height+'px';
    $('.clssearch').height(height);
    $('.divleft').height(height);
}
$( window ).resize(function() {
    initDivleft();
});
    //<![CDATA[
//    var map;
//    function initialize() {
//      if (GBrowserIsCompatible()) {
//        map = new GMap2(document.getElementById("map_canvas"));
//        map.setCenter(new GLatLng(10.82313524364468, 106.62963151931763), 15);
//         GEvent.addListener(map, "click", function(overlay,latlng) {
//          if (latlng) {
//               map.clearOverlays(); 
//               map.addOverlay(new GMarker(latlng));
//               $("#hdlat").val(latlng.lat());
//               $("#hdlng").val(latlng.lng());
//          }
//        });
//        var customUI = map.getDefaultUI();
//        customUI.maptypes.hybrid = false;
//        map.setUI(customUI);
//       
//      }
//    }
 
    //]]>
    
